package com.czl.elm.controller;


import com.czl.elm.domain.Seckill;
import com.czl.elm.domain.UsersLoginParam;
import com.czl.elm.rabbitmq.MQsend;
import com.czl.elm.service.ISeckillService;
import com.czl.elm.util.ActionResult;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/seckill")
public class SeckillController implements InitializingBean {
    @Autowired
    private ISeckillService seckillService;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private MQsend mQsend;

    Map<String,Boolean> checkFlag;
    @Override
    public void afterPropertiesSet() throws Exception {
        List<Seckill> res=seckillService.getAllSeckill();
        checkFlag=new HashMap<>();
        ValueOperations<String, Object> redisString = redisTemplate.opsForValue();
        for(Seckill seckill:res){
            redisString.set(seckill.getRedShopParperId(),seckill.getRedpaperNum());
            checkFlag.put(seckill.getRedShopParperId(),seckill.getRedpaperNum()>0?false:true);
        }
    }

    @ApiOperation("秒杀")
    @GetMapping("/doSeckill")
    public ActionResult doSeckill(String SeckillId){
        ValueOperations<String, Object> redisString = redisTemplate.opsForValue();
        String phoneName= ((UsersLoginParam) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getPhoneName();
        String activeId="seckill_"+SeckillId+phoneName;
        if(checkFlag.containsKey(SeckillId)&&checkFlag.get(SeckillId))return ActionResult.error("库存不足",500);
        if(redisString.get(activeId)!=null)return ActionResult.error("你已经抢到了",100);
        if(redisString.get(SeckillId)==null){
            Integer num=seckillService.getSeckillNum(SeckillId);
            if(redisString.get(SeckillId)==null)
                redisString.set(SeckillId,num);
        }
        Long seckillNum=redisString.decrement(SeckillId);

        if(seckillNum<0){
            redisString.set(SeckillId,0);
            checkFlag.put(SeckillId,true);
            return ActionResult.error("库存不足",500);
        }
        Map<String,String> msg=new HashMap<>();
        msg.put("SeckillId",SeckillId);
        msg.put("phoneName",phoneName);
        msg.put("id",activeId);
        mQsend.sendSeckillMsg(msg);
        return ActionResult.success("排队中",0);

    }

    @ApiOperation("轮询查询结果")
    @GetMapping("/getSeckillResult")
    public ActionResult getSeckillResult(String SeckillId){
        return seckillService.getSeckillResult(SeckillId);
    }


    @ApiOperation("获取秒杀剩余个数")
    @GetMapping("/SeckillNum")
    public ActionResult SeckillNum(String SeckillId){
        return seckillService.SeckillNum(SeckillId);
    }

    @ApiOperation("判断该用户是否抢过秒杀")
    @GetMapping("/checkSeckill")
    public ActionResult checkSeckill(String SeckillId){
        return seckillService.checkSeckill(SeckillId);
    }

    @ApiOperation("返回固定时段的秒杀list")
    @GetMapping("/seckillList")
    public ActionResult seckillList(String killTime,Integer currentPage,  Integer size){
        return seckillService.seckillList(killTime,currentPage,size);
    }

    @ApiOperation("返回固定时段的秒杀该用户情况list")
    @GetMapping("/seckillListbyuser")
    public ActionResult seckillListbyuser(String killTime){
        return seckillService.seckillListbyuser(killTime);
    }

}
